<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/control/BaseService.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/db/UserDAO.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/db/publish/PublishUserDAO.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/db/publish/PublishPayToUserDAO.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/util/Util.php';  

class UserService extends BaseService {
	
	public function __construct(){
		parent::__construct();
	 	parent::setTarget($this);	
	 	$this->log = Plog::factory(__FILE__);	
	}
	
	
	 
	
	public function login() {
		$account = $_POST["account"];
		$password = $_POST['password'];
		$userDAO = new UserDAO();
		
		$user = $userDAO->getUser($account, $password);
	  
		if ($user == null) { //無帳號
			Util::alert('帳號不存在,請重新登入!');
			Util::forward("/ad/html/publish/publishLogin.php?&account=$account&password=$password");
		} else if ($user->Type == 0 ) { //管理者
			@session_start();
			$_SESSION["user"] = Util::toJson($user);
			Util::forward("/ad/html/publish/publishMember.php");
		} else if ($user->Type == 3 ) { //開發人員
			@session_start();
			$_SESSION["user"] = Util::toJson($user);
			Util::forward("/ad/html/publish/publishMAppList.php");
		} else { //一般使用者
			@session_start();
			$_SESSION["user"] = Util::toJson($user);
			Util::alert('請以 管理者 或 開發者 帳號登入!');
			Util::forward("/ad/html/publish/publishLogin.php?&account=$account");
		} 
		 
	}

		/**
	 * 
	 * 開者者 新增使用者
	 */
	public function trgetInsert(){
		try {
			@session_start();
			if(isset($_SESSION["user"]))
				$user = Util::jsonToObject($_SESSION["user"]);
			 
			$nowDate =  Util::getNowDate();
			$param = array(
				"account" => $_POST["account"],
				"password" =>  $_POST["password"],
				"companyName" => $_POST["companyName"],
			    "count" => 0,
				"name" => $_POST["name"],
			    "phone" =>$_POST["phone"],
				"type" => "3",
				"status" => "1",
				"county" => $_POST["county"],
				"area"=> $_POST["area"],
				"zipcode"=>$_POST["zipcode"],
				"address"=>$_POST["address"],
				"createTime"=>$nowDate,
				"ModifyTime" => $nowDate,
				"ProxyAccount" => $user->{"Account"}
			);
			 	 
			$userDAO=  new UserDAO();
			$reuslt = $userDAO->insert($param);
			if ($reuslt == 1) {
				$obj = (Object) $param;
				$fdate = Util::getNowDate('Y/m/d');
				Util::email(
					$obj->account, 
					"Amazing Mobile ADs通知：會員$obj->name 已完帳號建立，請您立即啟用", 
					"親愛的 $obj->name 您好，<BR>

					   感謝您加入Amazing Mobile Ads廣告，您的會員資料已於<font color=red> $fdate </font> 建立產生<BR>
					 <b>您的帳號為:</b> $obj->account <BR>
					 <b>您的密碼為:$obj->password </b><BR>
					  歡迎您至<a href='http://mmedia.ai-ad.net.tw/ad/html/publish/publishLogin.php'>http://mmedia.ai-ad.net.tw/ad/html/publish/publishLogin.php</a> 登錄您的應用程式 <BR>
					  如果您對於廣告上有任何問題，請來信至: service@ai-ad.net.tw <BR>
					<BR>
					  謝謝您，<BR>
					 Amazing Mobile Ads敬上<BR>
					"
				);
			}
			$msg = $reuslt =="" ? "帳戶已被註冊!":"新增成功";
			//$user = $userDAO->getUser($_POST["account"], $_POST["password"]);
//			@session_start();
//			$_SESSION["user"] = Util::toJson($user);
			echo "{success:true,result:'$reuslt',msg :'$msg'}";
		} catch(Exception $e) {
			echo "{failure :true,result:'0', msg:'$e->getMessage()'}";
		}
	}
	 
	/**
	 * 管理員查詢所屬全部會員資料
	 */
	public function queryTargetAccountMember() {
		@session_start();
		$user = Util::jsonToObject($_SESSION["user"]);
		$name=null;
		if(isset($_POST['name'])){
			$name=$_POST['name'];
		}
		$userDAO = new PublishUserDAO();
//		echo "name=$name";
		$result = $userDAO->queryTargetAccountMember($user->{"Account"},$name);
//		echo "result=$result";
		if ($result[0]->{"account"}== null) {
			echo "{row:[]}";	
		} else {
			echo "{row:".Util::toJson($result)."}";	
		}
	}
	
	/**
	 * 管理員查詢所屬單一會員詳細資料
	 */
	public function getUser($managerAccount) {
		@session_start();
//		echo $_GET["managerAccount"];
		$_SESSION["settleAccount"]=$managerAccount;
//		echo $managerAccount;
		$userDAO = new PublishUserDAO();
	    $_SESSION["managerAccount"]=Util::toJson($userDAO->getUser($managerAccount));
//	    echo Util::toJson($userDAO->getUser($managerAccount));
		header("Location:/ad/html/publish/publishMemberFin.php");
	}
	public function getUserOverload(){
		$managerAccount=@$_GET["managerAccount"];
		$this->getUser($managerAccount);
		
	}
	
	/**
	 * 
	 * 取得該會員所有帳務資料
	 */
	public function queryAllPayToUserDateByAccount(){
		@session_start();
		$managerAccount=@$_SESSION["settleAccount"];
		$settleDAO = new PublishPayToUserDAO();
		$result=$settleDAO->queryAllDateByAccount($managerAccount);
//		echo $result;
		if ($result[0]->{"Account"}== null) {
			echo "{row:[]}";	
		} else {
			echo "{row:".Util::toJson($result)."}";	
			
		}
	}
	
	/**
	 * 更新銀行資訊
	 */
	public function saveUserBankInfo(){
		$managerAccount=@$_GET["managerAccount"];
		$bankDateChange=@$_GET["bankDateChange"];
		if($bankDateChange){
			$bankCode=@$_GET["BankCode"];
			$bankName=@$_GET["BankName"];
			$branchOfBank=@$_GET["BranchOfBank"];
			$bankAccount=@$_GET["BankAccount"];
			$userDAO = new PublishUserDAO();
			$userDAO->updateUserBankInfo($managerAccount, $bankCode,
	 						$bankName, $branchOfBank, $bankAccount);
		}
		$this->getUser($managerAccount);
	}
	
	public function logout() {
		@session_start();
		if (isset($_SESSION["user"])) {
			 unset($_SESSION["user"]);
		}
		if (isset($_SESSION["ad"])) {
			 unset($_SESSION["ad"]);
		}  
		Util::forward("/ad/html/publish/publishLogin.php"); 	 
	}
}

$service =  new UserService();
$service->__call($_GET["action"], null);
//$service->queryTargetAccountMember();
?>